const Ready = require('./ready.js');
class Playing extends Ready {
  constructor() {
    super();
    this.timeGroup = {};
    this.numGroup = {};
    this.gameNum = 0;
    this.MaxGameTime = 15;
    this.MaxGameNum = 30;
    this.ballsArr = [];
    this.balls = null;
  }
  initBallsPosiation() {
    let ballBox = game.add.group();
    let r = 138;
    let boxW = 3 * r
    ballBox.x = this.cx - (boxW / 2);
    ballBox.y = this.bh * (197 / 1200);
    ballBox.y -= r / 2;
    let boxH = this.bh * (553 / 1200);
    this.ballBoxHeight = boxH; //汤圆容器总高度
    this.ballCols = ~~(this.ballBoxHeight / r); //一共多少排汤圆
    
    while (boxH > r) {
      boxW = boxW - r
      this.ballsArr.push({
        x: boxW,
        y: boxH - r,
        show: false
      })
      if (boxW <= 0) {
        boxH = boxH - r;
        boxW = 3 * r;
      }
    }
    this.ballsArr.forEach(v => {
      // let ball=ballBox.create(v.x, v.y, 'circles');
    })
    this.balls = ballBox;
    // this.ballBox.setAll('inputEnabled',true);
    // balls.frame = ~~(8 * Math.random());
  }
  updateMans() {
    this.mans.setAll('alpha', 0);
    let beanMan = this.mans.getAt(2);
    beanMan.alpha = 0.3;
  }
  setBall() {
    let spriteLen = 9;
    let spriteIndex = ~~(Math.random() * spriteLen);
    let ballIndex = ~~(Math.random() * this.ballsArr.length);
    let ballData = this.ballsArr[ballIndex];
    let aliveTime = (~~(Math.random() * 2000) + 1000); //存在时间
    if (ballData.show == true) {
      // this.setBall();
      console.log("repeats");
      return
    }
    ballData.show = true;
    let player = this.balls.create(ballData.x, ballData.y, 'circles', spriteIndex);
    player.alpha = 0;
    game.add.tween(player).to({
      alpha: 1
    }, 400).start();
    player.inputEnabled = true;
    this.ballBoxHeight = this.bh * (553 / 1200);
    let flag=false;
    let aliveTimer = setTimeout(() => { //设置定时摧毁。
      ballData.show = false;
      player.destroy();
    }, aliveTime);
    player.events.onInputDown.add(v => {
      player.inputEnabled = false;
      clearTimeout(aliveTimer);
      let speed = 400 + ((~~ballIndex / 3) / this.ballCols);
      game.add.tween(player).to({
        y: this.ballBoxHeight,
        alpha: 0
      }, speed).start().onComplete.add(function () {
        ballData.show = false;
        player.destroy();
      });
      if(!flag){
        this.gameNum++;
        
      }
      flag=true;
      this.updateNumGroup();
    })
  }
  setBalls(times = 3) {
    for (let i = 0; i < times; i++) {
      this.setBall();
    }
  }
  setTimer() {
    let timer = game.time.create(false);
    timer.loop(1000, () => {
      this.gameTime--;
      if (this.gameTime < 0) {
        this.checkGame();
        return
      }
      this.timeGroup.getAt(4).text = this.gameTime;
      this.setBalls(3);
    });
    timer.start();
    let timer2 = game.time.create(false);
    timer2.loop(1500, () => {
      this.setBalls(3);
    });
    // timer2.start();
  }
  updateNumGroup() { // 计数显示
    let txt = this.numGroup.getAt(1);
    txt.text = 'x ' + this.gameNum;
    this.checkGame();
  }
  checkGame() {
    if (this.gameNum >= this.MaxGameNum) {
      this.reinitPlaying();
      game.state.start('Success');
    }
    if (this.gameTime <= 0 && this.gameNum < this.MaxGameNum) {
      this.reinitPlaying();
      game.state.start('Fails');
    }

  }
  reinitPlaying() {
    this.gameNum = 0;
    this.gameTime = this.MaxGameTime;
    this.updateNumGroup();
    this.timeGroup.getAt(4).text = this.gameTime;
  }
  create() {
    // game.physics.startSystem(Phaser.Physics.ARCADE);
    // game.physics.arcade.gravity.y = 2000;
    this.setBg();
    this.setBeanMan();
    this.updateMans();
    this.setTime();
    this.setTimer();
    this.setNum();
    this.initBallsPosiation();
    this.setBalls();
  }
}

module.exports = Playing;